package org.eduapp.trains.service.announce.impl;

import org.apache.log4j.Logger;
import org.eduapp.trains.DAOException;
import org.eduapp.trains.ServiceException;
import org.eduapp.trains.dao.announce.AnnounceDAO;
import org.eduapp.trains.model.announce.ModelAnnounce;
import org.eduapp.trains.service.announce.AnnounceService;
import org.eduapp.trains.utils.UtilsString;
import org.eduapp.trains.web.support.PaginationSupport;

/**
 * 
 * @author jeccy
 *
 */
public class AnnounceServiceImpl implements AnnounceService {
	
	private final static Logger logger = Logger.getLogger(AnnounceServiceImpl.class);
			
	private AnnounceDAO announceDAO;
	
	@Override
	public ModelAnnounce get(String id) throws ServiceException {
		if (UtilsString.isNotEmpty(id)) {
			try {
				return announceDAO.get(id);
			} catch (DAOException e) {
				logger.error("get annnounce by id" + id + ",caused by:" + e.getMessage());
				e.printStackTrace();
			} catch (Exception e) {
				logger.error("get annnounce by id" + id + ",caused by:" + e.getMessage());
				throw (new ServiceException(e));
			}
		} 
		return null;
	}

	@Override
	public boolean remove(String id) throws ServiceException {
		if (UtilsString.isNotEmpty(id)) {
			ModelAnnounce model = get(id);
			if (model != null) {
				try {
					announceDAO.remove(model);
					return true;
				} catch (DAOException e) {
					logger.error("Remove annnounce failed with id" + id + ",caused by:" + e.getMessage());
					e.printStackTrace();
				} catch (Exception e) {
					logger.error("Remove annnounce failed with id" + id + ",caused by:" + e.getMessage());
					throw (new ServiceException(e));
				}
			} else {
				logger.info("Remove announce failed, id with " + id + "not found");
			}
		}
		logger.info("Remove announce failed, empty id found");
		return false;
	}

	@Override
	public boolean saveOrUpdate(ModelAnnounce model) throws ServiceException {
		if (model != null) {
			try {
				announceDAO.saveOrUpdate(model);
				return true;
			} catch (DAOException e) {
				logger.error("SaveOrUpdate annnounce failed with model" + model.toString() + ",caused by:" + e.getMessage());
				e.printStackTrace();
			} catch (Exception e) {
				logger.error("SaveOrUpdate annnounce failed with model" + model.toString() + ",caused by:" + e.getMessage());
				throw (new ServiceException(e));
			}
		}
		logger.info("SaveOrUpdate announce failed, empty model found");
		return false;
	}
	
	@Override
	public PaginationSupport<ModelAnnounce> get(ModelAnnounce model,
			int pageSize, int offset, String order, Boolean isDesc)
			throws ServiceException {
		try {
			return announceDAO.get(model, pageSize, offset, order, isDesc);
		} catch (DAOException e) {
			logger.error("get annnounces by pager failed ,caused by:" + e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			logger.error("get annnounces by pager failed ,caused by:" + e.getMessage());
			throw (new ServiceException(e));
		}
		return null;
	}

	public void setAnnounceDAO(AnnounceDAO announceDAO) {
		this.announceDAO = announceDAO;
	}

	public AnnounceDAO getAnnounceDAO() {
		return announceDAO;
	}

	public static Logger getLogger() {
		return logger;
	}

}
